package com.youdelu.road;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.qozix.tileview.TileView;
import com.qozix.tileview.plugins.MarkerPlugin;
import com.stickerview.core.IMGPath;
import com.stickerview.core.sticker.IMGSticker;
import com.stickerview.core.sticker.IMGStickerPortrait;
import com.stickerview.view.JTDView;
import com.stickerview.view.StickerImageView;
import com.youdelu.road.adapter.JTDListAdapter;
import com.youdelu.road.bean.Attr;
import com.youdelu.road.bean.BgConf;
import com.youdelu.road.utils.AppConfig;
import com.youdelu.road.utils.BroadcastManager;
import com.youdelu.road.utils.Util;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener, IMGStickerPortrait.Callback,JTDListAdapter.ClickCallBack,JTDView.Callback{

    public static final String IMG_BGIMG = "bg_img";
    public static final String IMG_BGID = "bg_id";


    public static final String PUT_ATTR = "put_attr" ; // 添加控件
    public static final String UPDATE_ATTR = "update_attr" ; //更新控件
    public static final String Add_MAP = "add_map"; //打开方案
    public static final String MOVE_POSITION = "move_position"; //区域定位
    public static final String REFLUSH_MAP = "reflush_map"; //刷新地图

    /**
     * 打开文件管理器的类型
     */

    public static  final  int FILE_MOVE = 1 ;
    public static  final  int FILE_ADD = 2 ;
    public static  final  int FILE_SET = 3 ;
    private TileView mapView;

    private MarkerPlugin markerPlugin;
    private float mScale = 1f;

    private static String ASSET = "file:///android_asset/";

    private Button bgBtn;
    private Button selectBtn,addBtn,areaBtn;
    private Button settingBtn;
    private TextView shutdown;
    private TextView slidedown,tips;
    private Button jtdkz;
    private LinearLayout jtd_contorl,loading;

    private RelativeLayout tools,minitools;

    private Context mContext;
    private final  Activity activity = this ;
    private Gson gson;

    public static DbUtils db ;
    private int bg_id  = 0 ;

    private TextView close ;
    private ListView list;
    private JTDListAdapter adapter;
    private List<Attr> data = new ArrayList<>();

    private RadioGroup z_q,z_h,y_q,y_h,z_q_c,z_h_c,y_q_c,y_h_c;
    private EditText z_q_xs,z_h_xs,y_q_xs,y_h_xs,za_qb,xg_qb;
    private LinearLayout z_q_xs_l,z_h_xs_l,y_q_xs_l,y_h_xs_l,cd_z_q,cd_y_q,z_q_layout,y_q_layout;
    private Button z_q_xs_btn,z_h_xs_btn,y_q_xs_btn,y_h_xs_btn,za_qb_btn,xg_qb_btn;
    private ScrollView jtd_left_contorl;

    private TextView clear,cd_z_q_title,cd_y_q_title;

    private TextView btnJtd,btnJtd1,btnQbb,btnQbb1,btnHld,btnZxd;

    private LinearLayout qbb1,qbb2,fj_control;

    private int JTD_OPEN_TYPE = 0 ;

    /**
     * 选择第几个
     */
    private int z_q_select = 0 ,z_h_select = 0,y_q_select = 0,y_h_select = 0;

    /**
     * 隧道或桥梁 1 桥梁 2 隧道
     */
    private int qiaolianOrSuidao = 1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setContentView(R.layout.activity_main);
        mContext = this ;
        /**
         * 创建数据库 file_dirs
         */
        db = Util.init(mContext);

        gson = new Gson();
        init();
        initBordcast();
        startTimer();
    }

    public void startTimer(){
        new Handler().postDelayed(new Runnable(){
            public void run() {
                onViewScale();
                startTimer();
            }
        }, 3000);
    }

    /**
     * 地图加载完成
     */
    public void onReady(TileView tileView){
            markerPlugin = tileView.getPlugin(MarkerPlugin.class);
            initData();
    }

    /**
     * 添加点
     */

    public void addAttr(Attr attr){
        if(attr.getState()>0){
            addJtd(attr);
        }else{
            addSticker(attr);
        }
    }

    /**
     * 更新附件
     * @param attr
     */
    public void updateAttr(Attr attr){
        if(attr.getState()>0){
           int id = attr.getId();
           View view = getChildView(id);
           if(view!=null&&view instanceof JTDView){
               JTDView jtdView = (JTDView)view;
               markerPlugin.removeView(jtdView);
               addJtd(attr);
               onViewScaleDelay(300);
           }
        }
    }
    public void updateAttr_late(Attr attr){
        if(attr.getState()>0){
            int id = attr.getId();
            View view = getChildView(id);
            if(view!=null&&view instanceof JTDView){
                JTDView jtdView = (JTDView)view;
                markerPlugin.removeView(jtdView);
                addJtd(attr);
            }
        }
    }

    /**
     *  根据id获取view
     * @param id
     * @return
     */
    public View getChildView(int id){
        int count = markerPlugin.getChildCount();
        for(int i = 0 ; i < count ; i++){
            View  view = markerPlugin.getChildAt(i);
            if(view instanceof JTDView){
                try {
                    double[] d = (double[])view.getTag();
                    if(d!=null&&d.length>2){
                        if(d[2]==id){
                            return view;
                        }
                    }
                }catch (Exception e){
                    Log.e("getChildView",e.getMessage());
                }
            }
        }
        return null;
    }
    /**
     * 添加贴片
     * @param attr
     */
    public void addSticker(Attr attr){
        StickerImageView imageView = new StickerImageView(mContext);
        double x =  attr.getPointX();
        double y =  attr.getPointY();
        double[] coordinate = new double[]{x,y,attr.getId()};
        imageView.setTag(coordinate);
        IMGPath imgPath = new IMGPath();
        imgPath.setPath(attr.getPath());
        if(attr.getScale()!=0){
            imageView.setInitScale(attr.getScale());
        }
        imageView.setText(imgPath,attr.getFilp(),attr.getIs_lock());
        imageView.setRotation(attr.getRotation());
        imageView.registerCallback(this);
        markerPlugin.addMarker(imageView, (int)x, (int)y, 0.0f, 0.0f, 0, 0);
    }
    /**
     *
     */
    public void addJtd(Attr attr){
        JTDView jtdView = new JTDView(mContext);
        double x =  attr.getPointX();
        double y =  attr.getPointY();
        double[] coordinate = new double[]{x,y,attr.getId()};
        jtdView.setTag(coordinate);
        int state = attr.getState();
        if(state==1){
             if(attr.getFilp()==0){
                 if(attr.getRotation()==0){
                     state = 1 ;
                 }else{
                     state = 2 ;
                 }
             }else if(attr.getFilp()==1){
                 if(attr.getRotation()==0){
                     state = 2;
                 }else{
                     state = 1 ;
                 }
             }
        }
        if(attr.getType()==3){
            state = 13 ;
        }
        jtdView.setData(state,attr.getPath());
        jtdView.registerCallback(this);//注册点击事件
        markerPlugin.addMarker(jtdView, (int)x, (int)y, 0.5f, 0.5f, 0, 0);
    }

    /**
     * 地图触摸事件
     * @param
     */
    public void onMapTouch(MotionEvent event){
        //Log.e("position_",event.getX()+"/"+event.getY());
    }

    /**
     * 初始化数据
     */
    public void initData(){
        int count = markerPlugin.getChildCount();
        if(count>0){
            markerPlugin.removeAllViews();
        }
        String bgid = AppConfig.getInstance(mContext).get(IMG_BGID);
        if(bgid!=null){
            bg_id =  Integer.parseInt(bgid);
        }else{
            bg_id = 0 ;
        }
        String str = AppConfig.getInstance(mContext).get("bgconf_"+bg_id);
        if(str!=null){
            BgConf bgConf = gson.fromJson(str,BgConf.class);
            setPostion(bgConf);
        }else{
            BgConf bgConf = new BgConf();
            bgConf.setScaleX(1.0f);
            bgConf.setScaleY(1.0f);
            bgConf.setRotate(0.0f);
            bgConf.setScrollX(3560);
            bgConf.setScrollY(2482);
            bgConf.setScale(0.29335114f);
            setPostion(bgConf);
        }
        List<Attr> attrs = Util.findAttrList(db,bg_id);
        if(attrs!=null) {
            for (int i = 0 ; i < attrs.size();i++){
                addAttr(attrs.get(i));
            }
        }
        onViewScaleDelay(1000);
    }
    public void init() {
        TileView tileView = findViewById(R.id.mapView);
        bgBtn = this.findViewById(R.id.bg_btn);
        selectBtn = this.findViewById(R.id.select_btn);
        addBtn = this.findViewById(R.id.add_btn);
        settingBtn = this.findViewById(R.id.setting_btn);
        areaBtn = findViewById(R.id.area_btn);

        shutdown = this.findViewById(R.id.shutdown);
        slidedown = this.findViewById(R.id.slidedown);

        tools = this.findViewById(R.id.tools);
        minitools = this.findViewById(R.id.minitools);
        jtdkz = this.findViewById(R.id.jtdkz);
        jtd_contorl = this.findViewById(R.id.jtd_control);
        loading = this.findViewById(R.id.loading);

        btnJtd = this.findViewById(R.id.btnJtd);
        btnQbb = this.findViewById(R.id.btnQbb);
        btnJtd1 = this.findViewById(R.id.btnJtd1);
        btnQbb1 = this.findViewById(R.id.btnQbb1);
        btnHld = this.findViewById(R.id.btnHld);
        btnZxd = this.findViewById(R.id.btnZxd);

        z_q_xs_btn = this.findViewById(R.id.z_q_xs_btn);
        z_q_xs_btn.setOnClickListener(this);

        z_h_xs_btn = this.findViewById(R.id.z_h_xs_btn);
        z_h_xs_btn.setOnClickListener(this);

        y_q_xs_btn = this.findViewById(R.id.y_q_xs_btn);
        y_q_xs_btn.setOnClickListener(this);

        y_h_xs_btn = this.findViewById(R.id.y_h_xs_btn);
        y_h_xs_btn.setOnClickListener(this);

        za_qb_btn = this.findViewById(R.id.za_qb_btn);
        za_qb_btn.setOnClickListener(this);

        xg_qb_btn = this.findViewById(R.id.xg_qb_btn);
        xg_qb_btn.setOnClickListener(this);

        z_q_layout = this.findViewById(R.id.z_q_layout);
        y_q_layout = this.findViewById(R.id.y_q_layout);

        clear = this.findViewById(R.id.clear);
        WindowManager wm = getWindowManager();
        Display d = wm.getDefaultDisplay();

        //拿到布局参数
        ViewGroup.LayoutParams l = jtd_contorl.getLayoutParams();
        l.height=d.getHeight()/2;
        jtd_contorl.setLayoutParams(l);

        tips = this.findViewById(R.id.tips);

        bgBtn.setOnClickListener(this);
        selectBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        selectBtn.setOnClickListener(this);
        shutdown.setOnClickListener(this);
        settingBtn.setOnClickListener(this);
        slidedown.setOnClickListener(this);
        minitools.setOnClickListener(this);
        areaBtn.setOnClickListener(this);
        jtdkz.setOnClickListener(this);
        clear.setOnClickListener(this);

        btnJtd.setOnClickListener(this);
        btnQbb.setOnClickListener(this);
        btnJtd1.setOnClickListener(this);
        btnQbb1.setOnClickListener(this);
        btnHld.setOnClickListener(this);
        btnZxd.setOnClickListener(this);


        close = findViewById(R.id.close);
        close.setOnClickListener(this);
        list = findViewById(R.id.jtd_list);

        z_q = findViewById(R.id.z_q_radio);
        z_q.setOnCheckedChangeListener(this);

        z_q_c = findViewById(R.id.z_q_radio_c);
        z_q_c.setOnCheckedChangeListener(this);

        z_q_xs = findViewById(R.id.z_q_xs);
        z_h = findViewById(R.id.z_h_radio);
        z_h.setOnCheckedChangeListener(this);

        z_h_c = findViewById(R.id.z_h_radio_c);
        z_h_c.setOnCheckedChangeListener(this);

        z_h_xs = findViewById(R.id.z_h_xs);
        y_q = findViewById(R.id.y_q_radio);
        y_q.setOnCheckedChangeListener(this);

        y_q_c = findViewById(R.id.y_q_radio_c);
        y_q_c.setOnCheckedChangeListener(this);

        y_q_xs = findViewById(R.id.y_q_xs);
        y_h = findViewById(R.id.y_h_radio);
        y_h.setOnCheckedChangeListener(this);

        y_h_c = findViewById(R.id.y_h_radio_c);
        y_h_c.setOnCheckedChangeListener(this);

        y_h_xs = findViewById(R.id.y_h_xs);

        za_qb = findViewById(R.id.za_qb);
        xg_qb = findViewById(R.id.xg_qb);

        z_q_xs_l = findViewById(R.id.z_q_xs_l);
        z_h_xs_l = findViewById(R.id.z_h_xs_l);
        y_q_xs_l = findViewById(R.id.y_q_xs_l);
        y_h_xs_l = findViewById(R.id.y_h_xs_l);
        cd_z_q = findViewById(R.id.cd_z_q);
        cd_y_q = findViewById(R.id.cd_y_q);
        cd_z_q_title = findViewById(R.id.cd_z_q_title);
        cd_y_q_title = findViewById(R.id.cd_y_q_title);

        jtd_left_contorl = findViewById(R.id.jtd_left_contorl);

        fj_control = findViewById(R.id.fj_control);

        qbb1 = findViewById(R.id.qbb1);
        qbb2 = findViewById(R.id.qbb2);
        adapter = new JTDListAdapter(mContext, this, data);
        list.setAdapter(adapter);

        mapView = new TileView.Builder(tileView)
                .setSize(30000, 17500)
                .defineZoomLevel("map/tile-%1$d_%2$d.png")
                .installPlugin(new MarkerPlugin(this))
                .addReadyListener(this::onReady)
                .addTouchListener(this::onMapTouch)
                .addListener(new TileView.Listener() {
                    @Override
                    public void onScaleChanged(float scale, float previous) {
                        mScale = scale;
                        MarkerPlugin markerPlugin = mapView.getPlugin(MarkerPlugin.class);
                        int count = markerPlugin.getChildCount();
                        for(int i=0;i<count;i++){
                            if(markerPlugin.getChildAt(i) instanceof  JTDView){
                                JTDView jtdView = (JTDView)markerPlugin.getChildAt(i);
                                jtdView.setScale(mScale);
                            }
                        }
                        markerPlugin.onScaleChanged(scale, previous);
                    }

                    @Override
                    public void onZoomChanged(int zoom, int previous) {
                        //Log.e("position_onZoomChanged", zoom + "/" + previous);
                    }

                    @Override
                    public void onScrollChanged(int x, int y) {
                    }

                })
                .build();
    }

    /**
     * 交通灯选中事件
     * @param position
     */
    @Override
    public void select(int position) {
        Attr attr = adapter.getAttr(position);
        try {
            db.update(attr);
        }catch (Exception e){
            Log.e("update",e.getMessage());
        }
        attr.setBgId(bg_id);
        updateAttr(attr);
    }


    /**
     * 打开附件面板
     */
    public void openFJPanel(){
        slideDown();
        fj_control.setVisibility(View.VISIBLE);
    }
    public void closeFJPanel(){
        fj_control.setVisibility(View.GONE);
    }

    /**
     * 延迟刷新
     */
    public void onViewScaleDelay(final  int time){
        new Handler().postDelayed(new Runnable(){
            public void run() {
                onViewScale();
            }
        }, time);
    }
    public void onViewScale(){
        MarkerPlugin markerPlugin = mapView.getPlugin(MarkerPlugin.class);
        int count = markerPlugin.getChildCount();
        for(int i=0;i<count;i++){
            if(markerPlugin.getChildAt(i) instanceof  StickerImageView){
                StickerImageView stickerImageView = (StickerImageView)markerPlugin.getChildAt(i);
               stickerImageView.setLastScale();

            }else{
                JTDView jtdView = (JTDView)markerPlugin.getChildAt(i);
                jtdView.setScale(mScale);
            }
        }
        markerPlugin.onScaleChanged(mScale,0f);
        if(loading.getVisibility()==View.VISIBLE){
            loading.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化广播
     */
    public void initBordcast() {

        /**
         * 还原
         */
        BroadcastManager.getInstance(mContext).addAction(REFLUSH_MAP, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Integer id = intent.getIntExtra("id",0);
                if(id==bg_id){
                    initData();
                    Toast.makeText(context,"已还原",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context,"请先切换到默认再还原",Toast.LENGTH_SHORT).show();
                }
            }
        });

        /**
         *  添加附件
         */
        BroadcastManager.getInstance(mContext).addAction(PUT_ATTR, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String str = intent.getStringExtra("attr");
                if(str!=null){
                    Attr attr = gson.fromJson(str,Attr.class);
                    attr.setBgId(bg_id);
                    attr.setPointX(mapView.getScrollX());
                    attr.setPointY(mapView.getScrollY());
                    attr.setScale(0.5f);
                    attr.setIs_lock(1);
                    attr.setFilp(1);
                    put_attr(attr);
                }
            }
        });
        /**
         * 更新附件
         */
        BroadcastManager.getInstance(mContext).addAction(UPDATE_ATTR, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String str = intent.getStringExtra("attr");
                if(str!=null){
                    Attr attr = gson.fromJson(str,Attr.class);
                    attr.setBgId(bg_id);
                    updateAttr(attr);
                }
            }
        });
        /**
         *  切换方案
         */
        BroadcastManager.getInstance(mContext).addAction(Add_MAP, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String str = intent.getStringExtra("attr");
                if(str!=null){
                    Attr attr = gson.fromJson(str,Attr.class);
                    bg_id = attr.getBgId();
                    AppConfig.getInstance(mContext).set(IMG_BGID,String.valueOf(attr.getBgId()));
                    initData();
                }
            }
        });

        /**
         * 移动位置
         */
        BroadcastManager.getInstance(mContext).addAction(MOVE_POSITION, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String str = intent.getStringExtra("attr");
                if(str!=null){
                    Attr attr = gson.fromJson(str,Attr.class);
                    String data = attr.getPath();
                    BgConf bgConf = gson.fromJson(data,BgConf.class);
                    setPostion(bgConf);
                }
            }
        });
    }

    /**
     * 设置定位
     * @param bgConf
     */
    public void setPostion(BgConf bgConf){
        mScale = bgConf.getScale();
        mapView.setScaleX(bgConf.getScaleX());
        mapView.setScaleY(bgConf.getScaleY());
        mapView.smoothScrollTo(bgConf.getScrollX(),bgConf.getScrollY());
        mapView.setScale(bgConf.getScale());
    }

    /**
     * 获取定位
     * @return
     */
    public BgConf getPosition(){
        BgConf bgConf = new BgConf();
        bgConf.setScrollX(mapView.getScrollX());
        bgConf.setScrollY(mapView.getScrollY());
        bgConf.setScaleX(mapView.getScaleX());
        bgConf.setScaleY(mapView.getScaleY());
        bgConf.setScale(mapView.getScale());
        bgConf.setRotate(mapView.getRotation());
        return bgConf;
    }

    public void save_attr(Attr attr){
        attr.setPointX(attr.getPointX());
        attr.setPointY((float)attr.getPointY());
        attr.setBgId(bg_id);
        attr.setState(0);
        Util.save(db,attr);
    }
    /**
     * 放置附件
     */
    public void put_attr(Attr attr){
        attr.setPointX((mapView.getScrollX()+mapView.getMeasuredWidth()/3)/mScale);
        attr.setPointY((mapView.getScrollY()+mapView.getMeasuredHeight()/3)/mScale);
        addAttr(attr);
        onViewScaleDelay(300);
    }

    /**
     *
     * @param attr
     */
    public void del_attr(Attr attr){
        Util.delete(db,attr);
    }



    /**
     * 地图 按钮 点击 事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        String s = "";
        switch (v.getId()) {
            case R.id.bg_btn:
                openFileManager(FILE_MOVE);
                //openFJPanel();
                break;
            case R.id.select_btn:
                add_map_bg();
                break;
            case R.id.add_btn:
                openFileManager(FILE_ADD);
                break;
            case R.id.setting_btn:
                openFileManager(FILE_SET);
                break;
            case R.id.shutdown:
                checkreboot();
                break;
            case R.id.slidedown:
                slideDown();
                break;
            case R.id.minitools:
                RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) tools.getLayoutParams();
                lp1.setMargins(0,0,0,0);
                tools.setLayoutParams(lp1);
                break;

            case R.id.area_btn:
                add_position();
                break;
            case R.id.jtdkz:
                slideDown();
                open_jtd();
                break;
            case R.id.close:
                jtd_contorl.setVisibility(View.GONE);
                break;
            case R.id.clear:
                Util.delete_all(db,bg_id);
                String bgconf = gson.toJson(getPosition());
                AppConfig.getInstance(mContext).set("bgconf_"+bg_id,bgconf);
                initData();
                break;
            case R.id.btnJtd:
                openJtdControl();
                break;
            case R.id.btnJtd1:
                openJtd1Control();
                break;

            case R.id.btnQbb:
                openQbbControl();
                break;
            case R.id.btnQbb1:
                openQbb1Control();
                break;

            case R.id.btnHld:
                openHldControl();
                break;
            case R.id.btnZxd:
                openZxdControl();
                break;
                //交通灯全局控制
            case R.id.z_q_xs_btn:
                s = z_q_xs.getText().toString();
                startUpdateJtdSelected(2,4,s);
                break;
            case R.id.z_h_xs_btn:
                s = z_h_xs.getText().toString();
                startUpdateJtdSelected(1,4,s);
                break;
            case R.id.y_q_xs_btn:
                s = y_q_xs.getText().toString();
                startUpdateJtdSelected(3,4,s);
                break;
            case R.id.y_h_xs_btn:
                s = y_h_xs.getText().toString();
                startUpdateJtdSelected(4,4,s);
                break;
            case R.id.za_qb_btn:
                s = za_qb.getText().toString();
                startUpdateJtdSelected(1,13,s);
                break;
            case R.id.xg_qb_btn:
                s = xg_qb.getText().toString();
                startUpdateJtdSelected(3,13,s);
                break;
        }
    }
    public void startUpdateJtdSelected(int type,int i,String xs){
        if(loading.getVisibility()==View.GONE){
            loading.setVisibility(View.VISIBLE);
        }else{
            return;
        }
        updateJtdSelected(type,i,xs);
    }
    public void openJtdControl(){
        JTD_OPEN_TYPE = 0 ;
        qiaolianOrSuidao = 1;
        updateAllJtdList();
        clearAllBtn();
        jtd_left_contorl.setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_1).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_3).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_5).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_6).setVisibility(View.VISIBLE);


        z_h.findViewById(R.id.z_h_1).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_3).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_5).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_6).setVisibility(View.VISIBLE);


        y_q.findViewById(R.id.y_q_1).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_3).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_5).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_6).setVisibility(View.VISIBLE);

        y_h.findViewById(R.id.y_h_1).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_3).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_5).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_6).setVisibility(View.VISIBLE);

        btnJtd.setBackground(getDrawable(R.drawable.btn_sharp));
    }
    public void openJtd1Control(){
        JTD_OPEN_TYPE = 0 ;
        qiaolianOrSuidao = 2 ;
        updateAllJtdList();
        clearAllBtn();
        jtd_left_contorl.setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_1).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_3).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_5).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_6).setVisibility(View.VISIBLE);

        z_h.findViewById(R.id.z_h_1).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_3).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_5).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_6).setVisibility(View.VISIBLE);


        y_q.findViewById(R.id.y_q_1).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_3).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_5).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_6).setVisibility(View.VISIBLE);



        y_h.findViewById(R.id.y_h_1).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_3).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_5).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_6).setVisibility(View.VISIBLE);



        btnJtd1.setBackground(getDrawable(R.drawable.btn_sharp));
    }
    public void openQbbControl(){
        JTD_OPEN_TYPE = 1 ;
        qiaolianOrSuidao = 1 ;
        updateAllJtdList();
        clearAllBtn();
        jtd_left_contorl.setVisibility(View.VISIBLE);
        btnQbb.setBackground(getDrawable(R.drawable.btn_sharp));
        z_h.findViewById(R.id.z_h_4).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_4).setVisibility(View.VISIBLE);
        z_q_layout.setVisibility(View.GONE);
        y_q_layout.setVisibility(View.GONE);
        z_h_xs_l.setVisibility(View.VISIBLE);
        y_h_xs_l.setVisibility(View.VISIBLE);

        //z_q.findViewById(R.id.z_q_1).setVisibility(View.VISIBLE);

        //z_q.findViewById(R.id.z_q_3).setVisibility(View.VISIBLE);


        y_q.findViewById(R.id.y_q_1).setVisibility(View.VISIBLE);

        y_q.findViewById(R.id.y_q_3).setVisibility(View.VISIBLE);

        qbb1.setVisibility(View.VISIBLE);
        qbb2.setVisibility(View.VISIBLE);
    }
    public void openQbb1Control(){
        JTD_OPEN_TYPE = 1 ;
        qiaolianOrSuidao = 2 ;
        updateAllJtdList();
        clearAllBtn();
        jtd_left_contorl.setVisibility(View.VISIBLE);
        btnQbb1.setBackground(getDrawable(R.drawable.btn_sharp));

        z_q_layout.setVisibility(View.GONE);
        y_q_layout.setVisibility(View.GONE);


        //z_q.findViewById(R.id.z_q_1).setVisibility(View.VISIBLE);

        //z_q.findViewById(R.id.z_q_3).setVisibility(View.VISIBLE);

        //y_q.findViewById(R.id.y_q_1).setVisibility(View.VISIBLE);

        //y_q.findViewById(R.id.y_q_3).setVisibility(View.VISIBLE);

        qbb1.setVisibility(View.VISIBLE);
        qbb2.setVisibility(View.VISIBLE);

        cd_z_q.setVisibility(View.GONE);
        cd_y_q.setVisibility(View.GONE);

        cd_z_q_title.setVisibility(View.GONE);
        cd_y_q_title.setVisibility(View.GONE);
    }
    public void openHldControl(){
        JTD_OPEN_TYPE = 2 ;
        qiaolianOrSuidao = 1 ;
        updateAllJtdList();
        clearAllBtn();
        jtd_left_contorl.setVisibility(View.GONE);
        btnHld.setBackground(getDrawable(R.drawable.btn_sharp));
        z_q.findViewById(R.id.z_q_7).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_8).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_9).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_10).setVisibility(View.VISIBLE);

        z_h.findViewById(R.id.z_h_7).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_8).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_9).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_10).setVisibility(View.VISIBLE);

        y_q.findViewById(R.id.y_q_7).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_8).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_9).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_10).setVisibility(View.VISIBLE);

        y_h.findViewById(R.id.y_h_7).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_8).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_9).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_10).setVisibility(View.VISIBLE);

    }
    public void openZxdControl(){
        JTD_OPEN_TYPE = 4 ;
        qiaolianOrSuidao = 1 ;
        updateAllJtdList();
        clearAllBtn();
        jtd_left_contorl.setVisibility(View.GONE);
        btnZxd.setBackground(getDrawable(R.drawable.btn_sharp));
        z_q.findViewById(R.id.z_q_7).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_8).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_9).setVisibility(View.VISIBLE);
        z_q.findViewById(R.id.z_q_10).setVisibility(View.VISIBLE);

        z_h.findViewById(R.id.z_h_7).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_8).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_9).setVisibility(View.VISIBLE);
        z_h.findViewById(R.id.z_h_10).setVisibility(View.VISIBLE);

        y_q.findViewById(R.id.y_q_7).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_8).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_9).setVisibility(View.VISIBLE);
        y_q.findViewById(R.id.y_q_10).setVisibility(View.VISIBLE);

        y_h.findViewById(R.id.y_h_7).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_8).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_9).setVisibility(View.VISIBLE);
        y_h.findViewById(R.id.y_h_10).setVisibility(View.VISIBLE);
    }
    public void clearAllBtn(){
        btnJtd.setBackground(getDrawable(R.drawable.btn_sharp_null));
        btnQbb.setBackground(getDrawable(R.drawable.btn_sharp_null));
        btnJtd1.setBackground(getDrawable(R.drawable.btn_sharp_null));
        btnQbb1.setBackground(getDrawable(R.drawable.btn_sharp_null));
        btnHld.setBackground(getDrawable(R.drawable.btn_sharp_null));
        btnZxd.setBackground(getDrawable(R.drawable.btn_sharp_null));

        z_q_layout.setVisibility(View.VISIBLE);
        y_q_layout.setVisibility(View.VISIBLE);

        cd_z_q.setVisibility(View.VISIBLE);
        cd_y_q.setVisibility(View.VISIBLE);

        cd_z_q_title.setVisibility(View.VISIBLE);
        cd_y_q_title.setVisibility(View.VISIBLE);

        qbb1.setVisibility(View.GONE);
        qbb2.setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_1).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_2).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_3).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_4).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_5).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_6).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_7).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_8).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_9).setVisibility(View.GONE);
        z_q.findViewById(R.id.z_q_10).setVisibility(View.GONE);

        z_q_xs_l.setVisibility(View.GONE);

        z_h.findViewById(R.id.z_h_1).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_2).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_3).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_4).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_5).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_6).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_7).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_8).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_9).setVisibility(View.GONE);
        z_h.findViewById(R.id.z_h_10).setVisibility(View.GONE);

        z_h_xs_l.setVisibility(View.GONE);

        y_q.findViewById(R.id.y_q_1).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_2).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_3).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_4).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_5).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_6).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_7).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_8).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_9).setVisibility(View.GONE);
        y_q.findViewById(R.id.y_q_10).setVisibility(View.GONE);

       y_q_xs_l.setVisibility(View.GONE);


        y_h.findViewById(R.id.y_h_1).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_2).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_3).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_4).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_5).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_6).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_7).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_8).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_9).setVisibility(View.GONE);
        y_h.findViewById(R.id.y_h_10).setVisibility(View.GONE);

        y_h_xs_l.setVisibility(View.GONE);


    }
    public void open_jtd(){
        jtd_contorl.setVisibility(View.VISIBLE);
        openJtdControl();
//        Intent intent = new Intent();
//        intent.setClass(this,JTDActivity.class);
//        startActivity(intent);

    }

    /**
     * 设置交通灯编辑位置
     * @param id
     */
    public void set_jtd_selected(int id){
        //Log.e("set_jtd_selected",id+"");
        int position = -1 ;
        for(int i = 0 ; i < adapter.dirs.size();i++){
            if(id==adapter.dirs.get(i).getId()){
                position = i ;
                break;
            }
        }
        if(position!=-1){
            final int p = position ;
            list.post(new Runnable() {
                @Override
                public void run() {
                    adapter.setIndex(p);
                    adapter.notifyDataSetChanged();
                    list.smoothScrollToPosition(p);
                }
            });
        }

    }

    public void add_position(){
        slideDown();
        String bgconf = gson.toJson(getPosition());
        Intent intent = new Intent();
        intent.putExtra("bgconf",bgconf);
        intent.setClass(this,PointListActivity.class);
        startActivity(intent);
    }
    public void add_map_bg(){
        slideDown();
        save_all_data();
        Intent intent = new Intent();
        intent.setClass(this,MapBgListActivity.class);
        startActivity(intent);
    }

    /**
     * 打开文件管理器
     */
    public void openFileManager(int type){
        slideDown();
        Intent intent = new Intent();
        intent.setClass(this,FileManagerActivity.class);
        intent.putExtra(FileManagerActivity.ACTION_TYPE,type);
        startActivity(intent);
    }

    /**
     * 菜单回缩
     */
    public void slideDown(){
        int height = tools.getHeight();
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tools.getLayoutParams();
        lp.setMargins(0,0,0,-height);
        tools.setLayoutParams(lp);
    }

    public void checkreboot(){
        new AlertDialog.Builder(mContext)
                .setMessage("是否保存当前配置？")
                .setNegativeButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       save_all_data();
                        Util.shutdown(activity);
                    }
                })
                .setPositiveButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        AppConfig.getInstance(mContext).remove("bgconf_"+bg_id);
                        Util.shutdown(activity);
                    }
                }).show();
    }

    /**
     * 保存数据
     */
    public void save_all_data(){
        Util.delete_all(db,bg_id);
        List<Attr> stickers = Util.getSticker(mapView);
        for (Attr attr:stickers) {
            attr.setBgId(bg_id);
            save_attr(attr);
        }
        String bgconf = gson.toJson(getPosition());
        AppConfig.getInstance(mContext).set("bgconf_"+bg_id,bgconf);
        AppConfig.getInstance(mContext).set(IMG_BGID,String.valueOf(bg_id));
    }
    @Override
    public void onBackPressed() {
        // super.onBackPressed();//注释掉这行,back键不退出
    }
    @Override
    protected void onDestroy() {
        BroadcastManager.getInstance(mContext).destroy(PUT_ATTR);
        BroadcastManager.getInstance(mContext).destroy(Add_MAP);
        BroadcastManager.getInstance(mContext).destroy(REFLUSH_MAP);
        BroadcastManager.getInstance(mContext).destroy(UPDATE_ATTR);
        BroadcastManager.getInstance(mContext).destroy(MOVE_POSITION);

        super.onDestroy();

    }

    @Override
    public <V extends View & IMGSticker> void onDismiss(V stickerView) {

    }

    @Override
    public <V extends View & IMGSticker> void onShowing(V stickerView) {

    }

    @Override
    public <V extends View & IMGSticker> boolean onRemove(V stickerView) {
        MarkerPlugin markerPlugin = mapView.getPlugin(MarkerPlugin.class);
        markerPlugin.removeView(stickerView);
        return true;
    }

    @Override
    public void onMove(View v, float x, float y) {
        //double[] coordinate = (double[]) v.getTag();
//        int actualWidth = v.getWidth();
//        int actualHeight = v.getHeight();
       // double[] coordinate = new double[]{(x/mScale-actualWidth/2),(y/mScale-actualHeight/2)};
        double[] coordinate = new double[]{x/mScale,y/mScale};
        v.setTag(coordinate);
    }

    @Override
    public void onCopy(View v) {
        StickerImageView stickerImageView = (StickerImageView)v;
        String path = stickerImageView.getPath().getPath();
        Attr att = new Attr();
        double[] coordinate = (double[])stickerImageView.getTag();

        att.setPointX(coordinate[0]);
        att.setPointY(coordinate[1]+v.getHeight()+10);
        att.setRotation(stickerImageView.getRotation());
        att.setScale(stickerImageView.getScale());
        att.setFilp(1);
        att.setIs_lock(1);
        att.setState(0);
        att.setPath(path);
        addAttr(att);
        onViewScaleDelay(300);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(loading.getVisibility()==View.GONE){
            loading.setVisibility(View.VISIBLE);
        }else{
            return;
        }
        boolean hide = false ;
        int type = 0 ,i  =0 ;
        String xs = "";
        switch (group.getId()){
            case R.id.z_q_radio:
                type = 2 ;
                xs = z_q_xs.getText().toString();
                switch (group.getCheckedRadioButtonId()){
                    case R.id.z_q_1:
                        i = 1 ;
                        break;
                    case R.id.z_q_2:
                        i = 2 ;
                        break;
                    case R.id.z_q_3:
                        i = 3 ;
                        break;
                    case R.id.z_q_4:
                        i = 4 ;
                        break;
                    case R.id.z_q_5:
                        i=5;
                        break;
                    case R.id.z_q_6:
                        i=6;
                        break;
                    case R.id.z_q_7:
                        i=7;
                        break;
                    case R.id.z_q_8:
                        i=8;
                        break;
                    case R.id.z_q_9:
                        i=9;
                        break;
                    case R.id.z_q_10:
                        i=10;
                        break;
                }
                break;
            case R.id.z_h_radio:
                type = 1 ;
                xs = z_h_xs.getText().toString();
                switch (group.getCheckedRadioButtonId()){
                    case R.id.z_h_1:
                        i = 1 ;
                        break;
                    case R.id.z_h_2:
                        i = 2 ;
                        break;
                    case R.id.z_h_3:
                        i = 3 ;
                        break;
                    case R.id.z_h_4:
                        i = 4 ;
                        break;
                    case R.id.z_h_5:
                        i = 5 ;
                        break;
                    case R.id.z_h_6:
                        i = 6 ;
                        break;
                    case R.id.z_h_7:
                        i = 7 ;
                        break;
                    case R.id.z_h_8:
                        i = 8 ;
                        break;
                    case R.id.z_h_9:
                        i = 9 ;
                        break;
                    case R.id.z_h_10:
                        i=10;
                        break;
                }
                break;
            case R.id.y_q_radio:
                type = 3 ;
                xs = y_q_xs.getText().toString();
                switch (group.getCheckedRadioButtonId()){
                    case R.id.y_q_1:
                        i = 1 ;
                        break;
                    case R.id.y_q_2:
                        i = 2 ;
                        break;
                    case R.id.y_q_3:
                        i = 3 ;
                        break;
                    case R.id.y_q_4:
                        i = 4 ;
                        break;
                    case R.id.y_q_5:
                        i = 5 ;
                        break;
                    case R.id.y_q_6:
                        i = 6 ;
                        break;
                    case R.id.y_q_7:
                        i = 7 ;
                        break;
                    case R.id.y_q_8:
                        i = 8 ;
                        break;
                    case R.id.y_q_9:
                        i = 9 ;
                        break;
                    case R.id.y_q_10:
                        i=10;
                        break;
                }
                break;
            case R.id.y_h_radio:
                type = 4 ;
                xs = y_h_xs.getText().toString();
                switch (group.getCheckedRadioButtonId()){
                    case R.id.y_h_1:
                        i = 1 ;
                        break;
                    case R.id.y_h_2:
                        i = 2 ;
                        break;
                    case R.id.y_h_3:
                        i = 3 ;
                        break;
                    case R.id.y_h_4:
                        i = 4 ;
                        break;
                    case R.id.y_h_5:
                        i = 5 ;
                        break;
                    case R.id.y_h_6:
                        i = 6 ;
                        break;
                    case R.id.y_h_7:
                        i = 7 ;
                        break;
                    case R.id.y_h_8:
                        i = 8;
                        break;
                    case R.id.y_h_9:
                        i = 9;
                        break;
                    case R.id.y_h_10:
                        i=10;
                        break;
                }
                break;
            case R.id.z_q_radio_c:
                hide = true;
                switch (group.getCheckedRadioButtonId()){
                    case R.id.z_q_1_c:
                        z_q_select = 0 ;
                        break;
                    case R.id.z_q_2_c:
                        z_q_select = 3;
                        break;
                    case R.id.z_q_3_c:
                        z_q_select = 2;
                        break;
                    case R.id.z_q_4_c:
                        z_q_select = 1;
                        break;
                }
                break;
            case R.id.z_h_radio_c:
                hide = true;
                switch (group.getCheckedRadioButtonId()){
                    case R.id.z_h_1_c:
                        z_h_select = 0 ;
                        break;
                    case R.id.z_h_2_c:
                        z_h_select = 6;
                        break;
                    case R.id.z_h_3_c:
                        z_h_select = 5;
                        break;
                    case R.id.z_h_4_c:
                        z_h_select = 4;
                        break;
                }
                break;
            case R.id.y_q_radio_c:
                hide = true;
                switch (group.getCheckedRadioButtonId()){
                    case R.id.y_q_1_c:
                        y_q_select = 0 ;
                        break;
                    case R.id.y_q_2_c:
                        y_q_select = 4;
                        break;
                    case R.id.y_q_3_c:
                        y_q_select = 5;
                        break;
                    case R.id.y_q_4_c:
                        y_q_select = 6;
                        break;
                }
                break;
            case R.id.y_h_radio_c:
                hide = true;
                switch (group.getCheckedRadioButtonId()){
                    case R.id.y_h_1_c:
                        y_h_select = 0 ;
                        break;
                    case R.id.y_h_2_c:
                        y_h_select = 1;
                        break;
                    case R.id.y_h_3_c:
                        y_h_select = 2;
                        break;
                    case R.id.y_h_4_c:
                        y_h_select = 3;
                        break;
                }
                break;

        }
        if(hide){
            loading.setVisibility(View.GONE);
        }else {
            updateJtdSelected(type, i, xs);
        }
    }
    private List<Attr> tempdata ;
    public void updateJtdSelected(int type,int i,String xs){
        //filp 方向  rotation 前后
        String filp="-1",rotation="-1";
        int position = 0 ;
        if(type==1){
            filp = "0";
            rotation="0";
            position = z_h_select;
        }else if(type==2){
            filp = "0";
            rotation="1";
            position = z_q_select;
        }else if(type==3){
            filp = "1";
            rotation="1";
            position = y_q_select;
        }else if(type==4){
            filp = "1";
            rotation="0";
            position = y_h_select;
        }
        if(i==7){
            i = 9 ;
        }else if(i==8){
            i = 10 ;
        }else if(i==9){
            i=11;
        }else if(i==10){
            i=12;
        }
        if(type==2||type==4){
            if(i==5){
                i = 7 ;
            }else if(i==6){
                i = 8 ;
            }
        }


        //Log.e("position",position+"/"+JTD_OPEN_TYPE);
        try{
            if(i==13){
                tempdata = db.findAll(Selector.from(Attr.class).where("state", ">", "0").and("filp","=",filp).and("type","=",3).and("position","=",qiaolianOrSuidao).and("bgId","=",bg_id));
            }else{
                if((JTD_OPEN_TYPE==0||JTD_OPEN_TYPE==1)&&position!=0){
                    tempdata = db.findAll(Selector.from(Attr.class).where("state", ">", "0").and("filp","=",filp).and("rotation","=",rotation).and("type","=",JTD_OPEN_TYPE).and("position","=",qiaolianOrSuidao).and("bgId","=",bg_id).and("title","like","%-"+position));
                }else{
                    tempdata = db.findAll(Selector.from(Attr.class).where("state", ">", "0").and("filp","=",filp).and("rotation","=",rotation).and("type","=",JTD_OPEN_TYPE).and("position","=",qiaolianOrSuidao).and("bgId","=",bg_id));
                }
            }
         }catch (Exception e){

        }finally {
            try {
            for(int a = 0 ; a < tempdata.size();a++){
                Attr attr = tempdata.get(a);
                attr.setPath(xs);
                attr.setState(i);
                updateAttr_late(attr);
                db.update(attr);
            }
            }catch (Exception e){

            }finally {
                updateAllJtdList();
            }

        }

    }
    public void updateAllJtdList(){
        try {
            if(JTD_OPEN_TYPE==1) {
                data = db.findAll(Selector.from(Attr.class).where("state", ">", "0").and("type", "=", JTD_OPEN_TYPE).or("type", "=", 3).and("bgId", "=", bg_id));
            }else if(JTD_OPEN_TYPE==0&&qiaolianOrSuidao==2){
                data = db.findAll(Selector.from(Attr.class).where("state", ">", "0").and("type","=",JTD_OPEN_TYPE).or("type","=",4).and("bgId","=",bg_id));
            }else{
                data = db.findAll(Selector.from(Attr.class).where("state", ">", "0").and("type","=",JTD_OPEN_TYPE).and("bgId","=",bg_id));
            }
            adapter.update(data);
        }catch (Exception e){

        }finally {
            onViewScaleDelay(300);
        }
    }

    /**
     * 点击交通灯
     * @param v
     */
    @Override
    public void onSelect(View v) {
        double[] coordinate =(double[])v.getTag();
        set_jtd_selected((int)coordinate[2]);
    }
}
